/*-----------------------------------------------------------------------------
-通用开发平台:用按钮导航
-作者：张金军
-所有人：张金军
-版权：版权所有，张金军，2005-2015，保留所有权利
-----------------------------------------------------------------------------*/
Yacon.widget.ButtonPilot = function (conText)
{
	this.elementID = "";
	
	this.childs  = new Array();
	
	Yacon.apply(this,conText);
	
	this.create();	
}

Yacon.widget.ButtonPilot.Extend(Yacon.widget.WinControl,{
	create:function()
	{

		this.element            = document.all[this.elementID];

		if (!this.element )
			return;
		
		this.element.Control    = this;	
		this.relation           = document.all[this.element.getAttribute("relationID")];
			
		var index =0;
		var cell = this.element.rows[0].cells[this.element.rows[0].cells.length-1];
		for(var  i= 0; i <cell.children.length;i++)
		{
			if (cell.children[i].tagName.toUpperCase() == "SPAN")
			{
				this.childs[index]         = cell.children[i];
				this.childs[index].Control = this;
				Yacon.addEvents(this.childs[index],"click",this.onButtonPilotClick);
				index ++
			}				
		}
	},
	onButtonPilotClick :function()
	{
		var eventElement;
		if (event.srcElement.tagName.toUpperCase()== "SPAN")
			eventElement = event.srcElement;
		if (event.srcElement.tagName.toUpperCase()== "IMG" ||event.srcElement.tagName.toUpperCase()== "LABEL")
			eventElement = event.srcElement.parentNode;
		
		if (!eventElement||!eventElement.Control||!eventElement.Control.relation)
			return;
		
		var relaControl = eventElement.Control.relation.Control;
		if (!relaControl ||!relaControl.dataSet)
			return;
		
		window["viewbuttonid"] = eventElement.getAttribute("deployID");
		switch(eventElement.getAttribute("deployID"))
		{
			case "add":
			{
				if (relaControl.materGrid && relaControl.materGrid.dataSet.getRecordCount()<=0)
					return;
						
				if (Yacon.isTrue(eventElement.getAttribute("isprfuntion")))
				{
					if (Yacon.isfireuserEvent(relaControl.elementID + "_beforeInsert",[relaControl.dataSet]))
						if (!Yacon.fireUserEvent(relaControl.elementID + "_beforeInsert",[relaControl.dataSet]))break;
					
					
					relaControl.dataSet.createRecord();					
									
					Yacon.fireUserEvent(relaControl.elementID + "_afterInsert",[relaControl.dataSet]);
				
				}
				break;				
			}
			case "edit":
			{
				if (Yacon.isTrue(eventElement.getAttribute("isprfuntion")))
				{
					
					if (Yacon.isfireuserEvent(relaControl.elementID + "_beforeEdit",[relaControl.dataSet]))
						if (!Yacon.fireUserEvent(relaControl.elementID + "_beforeEdit",[relaControl.dataSet]))break;
				
					relaControl.dataSet.editRecord();
				
					Yacon.fireUserEvent(relaControl.elementID + "_afterEdit",[relaControl.dataSet]);
					
				}
				break;				
			}
			case "delete":
			{
				
				if (Yacon.isTrue(eventElement.getAttribute("isprfuntion")))
				{
					var confirmTitle = "" ;		
					var eventConfirm = eventElement.childNodes[1].confirm ;
					if(eventConfirm==undefined || eventConfirm==null){
						confirmTitle = window._CONFIRM_DELETE_RECORD;
					}else{
						confirmTitle = eventConfirm ;
					}
										
					if (!confirmTitle)
						break;					
				
					if(relaControl.ischeck)
						confirmTitle = confirmTitle.replace("$","所选中");
					else
						confirmTitle = confirmTitle.replace("$","");
					
					if(relaControl.isCheck()){
						var checknum = relaControl.getCheckDataSet().getRecordCount() ;
						if(checknum==0){
							alert("未选中任何数据！请点击复选框选择要操作的信息");
							return ;
						}
					}else{
						var rnum = relaControl.getActiveRow();
						if(rnum==null || rnum==undefined){
							return ;
						}
					}
					
					//回调定义
					Yacon.fireUserEvent(relaControl.elementID + "_confirmTitle",[relaControl]);
					if(window.CONFIRM_FLAG == 1){
						confirmTitle = window.CONFIRM_TITLE_FIX;
					}
					
					if (confirm(confirmTitle))
					{					
						relaControl.deleteRecord();
						
						Yacon.fireUserEvent(relaControl.elementID + "_afterDelete",[relaControl]);
					}
					
				}
				
				/*eval("("+relaControl.elementID+".refresh();)");*/
				
				break;				
			}
			case "cancle":
			{
				if (Yacon.isTrue(eventElement.getAttribute("isprfuntion")))
				{
					//点击撤销时的动作，判断当前选中的信息是否修改过
					if (relaControl.dataSet.getState()=="past"){
						return ;
					}
					var confirmTitle = window._CONFIRM_CANCEL_RECORD;
					if (!confirmTitle)
						break;
									
					if(relaControl.ischeck)
						confirmTitle = confirmTitle.replace("$","所选中");
					else
						confirmTitle = confirmTitle.replace("$","");
					
					if (Yacon.isfireuserEvent(relaControl.elementID + "_beforeCancel",[relaControl.dataSet]))
						if (!Yacon.fireUserEvent(relaControl.elementID + "_beforeCancel",[relaControl.dataSet]))break;
				
					if (confirm(confirmTitle))
					{
						eventElement.Control.relation.Control.dataSet.cancelRecord();
							
						Yacon.fireUserEvent(relaControl.elementID + "_afterCancel",[relaControl.dataSet]);
					}
				}
				
				break;				
			}
			case "save":
			{
				if (Yacon.isTrue(eventElement.getAttribute("isprfuntion")))
				{
					if (Yacon.isfireuserEvent(relaControl.elementID + "_beforeSave",[relaControl.dataSet]))
						if (!Yacon.fireUserEvent(relaControl.elementID + "_beforeSave",[relaControl.dataSet]))break;
					
								
					eventElement.Control.relation.Control.dataSet.post(true);
					
					Yacon.fireUserEvent(relaControl.elementID + "_afterSave",[relaControl.dataSet]);
				}
				break;				
			}
			case "excel":
			{
				
				var exDataSet = getDatasetByID("excel");
				exDataSet.addAttribute("isexcel","true");
				
				Yacon.fireUserEvent(relaControl.elementID + "_beforeExcel",[exDataSet]);
				
				if (exDataSet.switchMessage(""))
				{
					window.open(window.contextPath  + exDataSet.getDataValue());
				}
				exDataSet =null;
				break;				
			}
			
		}
		
		if (Yacon.isfireuserEvent(eventElement.Control.elementID+"_" + eventElement.id+"_onclick"))
				Yacon.fireUserEvent(eventElement.Control.elementID+"_" +eventElement.id+"_onclick",[relaControl]);
		else if (Yacon.isfireuserEvent(eventElement.getAttribute("onViewClick")))
			 	Yacon.fireUserEvent(eventElement.getAttribute("onViewClick"));		
		
		
		
		window["viewbuttonid"] = null;
	}
	
});



window._CONFIRM_DELETE_RECORD="您确定要删除当前$记录吗?";
window._CONFIRM_CANCEL_RECORD="您确定要撤消对当前$记录的修改吗？";

